// 
// LWWSInfo.pde
// 
// obtain and store weather forcast information 
// from LWWS (Livedoor Weather Web Service)
// 
// LWWS (Livedoor お天気サービスから情報を受信し保存するクラス
// 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

class LWWSInfo {
  String  forecastDate;
  String dayOfTheWeek;
  String maxTemp;
  String minTemp;
  float x;
  float y;
  int w;
  int h;
  int step;
  int count;
  String imageURL;
  PImage imageObj;
  
  private PImage weatherImg;
  private int minCount = 27; // moveがこの回数を超えて呼び出されたら、アイコンが左移動を開始する
  
  
  // Constructor (1)
  //
  // access LWWS and store weather forcast information
  // LWWSにアクセスして天気予報情報を取得し保持する
  LWWSInfo( String _forecastDate, int _step, int _x, int _y ) throws IOException
  {
    forecastDate = _forecastDate;
    step = _step;
    x = _x ;
    y = _y ;
    count = 0;
    try
    {
      accessLWWS();
      weatherImg =  loadImage(imageURL);

    }
    catch (Exception e )
    {
      throw new IOException();
    }
    convertImage();
  }  

  // Constructor (2)
  //
  // テキストファイルなどに保存された天気予報情報をインスタンスに展開する
  LWWSInfo( String _forecastDate, String _dayOfTheWeek, String _maxTemp, String _minTemp, float _x, float _y, int _step, int _count, String _imageURL )
  {
    forecastDate = _forecastDate;
    dayOfTheWeek = _dayOfTheWeek;
    maxTemp = _maxTemp;
    if ( "null".equals(_minTemp)) {
      minTemp ="--";
    } else {
      minTemp = _minTemp;
    }
    x = _x;
    y = _y;
    step = _step;
    count = _count;
    imageURL = _imageURL;

    weatherImg =  loadImage(imageURL);
    w = weatherImg.width;
    h = weatherImg.height;
    convertImage();
  }
  
  //
  // display()
  //
  // 天気情報を指定の位置に表示するメソッド
  void display() {
    textSize(14);
    String s = maxTemp + " / " + minTemp;
    int diff = 1;
    image(imageObj, x, y);
    fill(64);
    text ( s, x + diff , y + 45 + diff); 
    fill(255);
    text ( s, x , y + 45); 

    textSize(12);
    s = dayOfTheWeek;
    fill(63,75,139);
    text ( s, x + diff , y + 65 + diff); 
    fill(145,160,240);
    text ( s, x , y + 65); 

  }

  //
  // accessLWWS()
  //
  // LWWSから取得した天気予報データを解析・保存する
  void accessLWWS(){
    JSONObject json;

    // Livedoor Weather Web Service / LWWS
    // xml = new XMLElement((PApplet)parent, "http://weather.livedoor.com/forecast/webservice/rest/v1?city=63&day=today");
    json = loadJSONObject("http://weather.livedoor.com/forecast/webservice/json/v1?city=130010");
  
    JSONArray forecasts = json.getJSONArray("forecasts");
    JSONObject tommorow = forecasts.getJSONObject(1);
    String date  = tommorow.getString("date"); 

    maxTemp = tommorow.getJSONObject("temperature").getJSONObject("max").getString("celsius");
    minTemp = tommorow.getJSONObject("temperature").getJSONObject("min").getString("celsius");
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    try {
      Date dt = sdf.parse(date);
      sdf.applyPattern("MMM-d");
      dayOfTheWeek = sdf.format(dt);
    }catch (Exception e) {
      println("caught e");
      println(e.toString());
    }
    

    if (minTemp == null) {
      minTemp="--";
    }
    imageURL = tommorow.getJSONObject("image").getString("url");
    w = tommorow.getJSONObject("image").getInt("width");
    h = tommorow.getJSONObject("image").getInt("height");
    
    // DEBUG
    println(dayOfTheWeek + " " + maxTemp + " / " + minTemp);
    println(imageURL);
    println ("w:" + w + " h:" + h);
    // DEBUG END

  }
  
  //
  // move()
  //
  // 画像の表示位置をx軸上左に移動させる
  void move () 
  {
    count++;
    if ( count > minCount ) {
      x -= step;
    }
  }

  //
  // convertImage()
  //
  // 天気データの画像イメージを変換する
  // 背景色を透明に変更する(元画像の背景色が白(255)である前提)
  void convertImage ()
  {

    PImage img2 = createImage(w, h, ARGB);
    img2.loadPixels();
    weatherImg.loadPixels();

    color blendedColor;

    for ( int i=0 ; i < weatherImg.pixels.length ; i++){
      boolean isEdge = false ;
      // println ("   pixcel "+i + ": " + weatherImg.pixels[i]);

      // if background (assume white)
      if ( weatherImg.pixels[i] == color(255,255,255) ){
        img2.pixels[i] = color(255,0);
      } 
      else 
      {
        // left edge
        if ( i < weatherImg.pixels.length -1  && weatherImg.pixels[i+1] == color(255,255,255)) {
          isEdge = true;
        } 
        // right edge
        else if ( i > 0  && weatherImg.pixels[i-1] == color(255,255,255)) {
          isEdge = true;
        }
        // top edge
        else if ( (i - weatherImg.width) >= 0   && weatherImg.pixels[i-weatherImg.width] == color(255,255,255)) {
          isEdge = true;
        }
        // bottom edge
        else if (( (i + weatherImg.width) < weatherImg.pixels.length   && weatherImg.pixels[i+weatherImg.width] == color(255,255,255)) || (i + weatherImg.width) > weatherImg.pixels.length ) {
          isEdge = true;
        }
        if (isEdge) {
          float a_value = (255 - saturation(weatherImg.pixels[i])) ;
          // println ("i " + i + " / a_value:" +a_value); 
          img2.pixels[i] = color (red(weatherImg.pixels[i]), green(weatherImg.pixels[i]), blue(weatherImg.pixels[i]), a_value);
        }
        else {
          img2.pixels[i] = weatherImg.pixels[i];
        }
      }
    }
    img2.updatePixels();
    imageObj = img2;
  }
}


